//
//  MenuUsuallyView.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/12/7.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MenuUsuallyView.h"
#import "MenuCenterCell.h"
#import "UIView+ZF.h"
#import "HomeModel.h"
#import "MenuUsuallyTableView.h"
#define kRowH 44
@interface MenuUsuallyView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) MenuUsuallyTableView *usuallyTabView;
@property(nonatomic,strong) NSMutableArray *dataMutb;
@property(nonatomic,assign) NSInteger index;
@end
@implementation MenuUsuallyView
static NSString *identifierMenuCenterCell = @"menuCenterCell";
-(NSMutableArray *)dataMutb
{
    if (!_dataMutb) {
        _dataMutb = [NSMutableArray array];
    }
    return _dataMutb;
}
-(void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    
}
- (MenuUsuallyTableView *)usuallyTabView
{
    if (!_usuallyTabView) {
        CGRect frame = CGRectMake(0.f, 0, self.w, self.h);
        _usuallyTabView = [[MenuUsuallyTableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        _usuallyTabView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _usuallyTabView.delegate = self;
        _usuallyTabView.dataSource = self;
        _usuallyTabView.backgroundColor = [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1];
        [_usuallyTabView registerNib:[UINib nibWithNibName:@"MenuCenterCell" bundle:nil] forCellReuseIdentifier:identifierMenuCenterCell];
    }
    return _usuallyTabView;
}
- (void)setH:(CGFloat)h
{
    _h = h;
    _usuallyTabView.h = h;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
//        self.backgroundColor = kOrangeBg;
        [self addSubview:self.usuallyTabView];
        [self reloadInternetData];
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usuallyplacelist:) name:@"usuallyplacelist" object:nil];
    }
    return self;
}
-(void)usuallyplacelist:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSArray *temp = [dict objectForKey:@"date"];
    for (NSDictionary *dict in temp) {
        HomeModel *model = [HomeModel mj_objectWithKeyValues:dict];
        [self.dataMutb addObject:model];
    }
    [self.usuallyTabView reloadData];
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)reloadInternetData
{
    [CustomeManager POSTWithURLIndex:38 parameters:nil success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataMutb.count;
}
-(void)reloadData
{
    [self.usuallyTabView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    MenuCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierMenuCenterCell forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1];
    HomeModel *model = self.dataMutb[indexPath.row];
    cell.usuallyShow = self.usuallyShow;
    cell.usuallyModel = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kRowH;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HomeModel *model = self.dataMutb[indexPath.row];
    HomeModel *modelPre = self.dataMutb[self.index];
    if (self.index != indexPath.row) {
        modelPre.show = NO;
    }
    model.show = YES;
    self.index = indexPath.row;
    if (self.returnValueBlock) {
        self.returnValueBlock(model.content);
    };
}
@end
